package com.aaa.arouterone.testOcr.utils

import android.content.Context
import android.graphics.Bitmap
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.tamsiree.rxkit.RxDeviceTool
import com.tamsiree.rxkit.RxTool

/**
 * @Description:
 * @Author: lfc
 * @Email: iamlifuchang@163.com
 * @CreateTime: 2021/4/2 10:47
 * @UpdateRemark: 尝试等比例展示图片
 */
public class ImgShowUtils {
    fun showImgs(ctx: Context, iv: ImageView, strUrl: String) {
        Glide.with(ctx).asBitmap().load(strUrl).into(object : SimpleTarget<Bitmap>() {
            override fun onResourceReady(
                resource: Bitmap,
                transition: com.bumptech.glide.request.transition.Transition<in Bitmap>?
            ) {
                val imageWidth = resource.width
                val imageHeight = resource.height
                val width: Int = RxDeviceTool.getScreenWidth(ctx) //固定宽度

                //宽度固定,然后根据原始宽高比得到此固定宽度需要的高度
                val height = width * imageHeight / imageWidth
                val para: ConstraintLayout.LayoutParams =
                    iv.layoutParams as ConstraintLayout.LayoutParams
                para.height = height
                para.width = width
                iv.setImageBitmap(resource)
            }
        })

    }

}